package com.xiaoma.verbal.service;

import java.util.List;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.xiaoma.tpo.domain.User;
import com.xiaoma.tpo.resource.TPOConstants;
import com.xiaoma.verbal.dao.PayAudioDao;
import com.xiaoma.verbal.dao.QuQuestionDao;
import com.xiaoma.verbal.domain.PayAudio;
import com.xiaoma.verbal.domain.QuQuestion;

@Service
public class PayAudioService {

    @Autowired
    private PayAudioDao payAudioDao;
    
    @Autowired
    private  RestTemplate restTemplate;
    
    @Autowired
    private QuQuestionDao quQuestionDao;
    
    public void save(PayAudio payAudio) {
        int id = payAudioDao.insert(payAudio);
        payAudio.setId(id);
    }
    
    public void update(PayAudio payAudio) {
        payAudioDao.update(payAudio);
    }
    
    public List<PayAudio> findByUserId(int userId) {
        PayAudio entity = new PayAudio();
        entity.setUserId(userId);
        return payAudioDao.findByColumns(entity);
    }
    
    public PayAudio getById(int id) {
        return payAudioDao.getById(id);
    }
    
    public boolean post2Remote(PayAudio audio, User user){
        String applyTicketUrl = TPOConstants.XMPG + "/xmpg/service/applyTicket?productionName=" + TPOConstants.PRODUCTION_NAME + "&count=1";
        JSONObject json = restTemplate.getForObject(applyTicketUrl, JSONObject.class);
        String identifier = json.getJSONArray("tickets").getJSONObject(0).getString("identifier");
        //tijiao
        Integer questionId = audio.getQuestionId();
        QuQuestion question = quQuestionDao.getById(questionId);
        if (question != null) {
            String content = question.getContent();
            JSONObject exerciseJson = new JSONObject();
            exerciseJson.put("productionName", TPOConstants.PRODUCTION_NAME);
            exerciseJson.put("moduleName", TPOConstants.MODULE_NAME);
            exerciseJson.put("type", 1);
            exerciseJson.put("questionId", questionId);
            exerciseJson.put("questionSeq", questionId);
            exerciseJson.put("question", content);
            exerciseJson.put("answerId", audio.getId());
            exerciseJson.put("answer", audio.getAudioUrl());
            exerciseJson.put("answerAudioLength", audio.getAudioLength());
            exerciseJson.put("studentId", user.getId());
            exerciseJson.put("studentNickName", user.getNickname());
            exerciseJson.put("studentAvatar", user.getAvatar());
            exerciseJson.put("studentPhone", user.getPhone());
            exerciseJson.put("studentEmail", user.getEmail());
            exerciseJson.put("ticket", identifier);
            exerciseJson.put("submitTime", System.currentTimeMillis());
            String submitExerciseUrl = TPOConstants.XMPG + "/xmpg/service/submitExercise";
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<JSONObject> entity = new HttpEntity<JSONObject>(exerciseJson, headers);
            ResponseEntity<JSONObject> responseEntity= restTemplate.exchange(submitExerciseUrl, HttpMethod.POST, entity, JSONObject.class);
            JSONObject jsonObj = responseEntity.getBody();
            return jsonObj.getInt("status") == 0;
        }
        return false;
    }
    
}
